home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / plot2d / g2d5.jav < prev    next >
Encoding:
Text File  |  1996-01-11  |  3.2 KB  |  131 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.net.URL;
  4.  
  5. public class g2d5 extends Applet {
  6.  
  7.       Graph2D graph;
  8.       DataSet data1;
  9.       Axis    xaxis;
  10.       Axis    yaxis;
  11.       TextField period;
  12.       TextField amplitude;
  13.       int  period_new    = 1;
  14.       int  amplitude_new = 1;
  15.       Panel range;
  16.       String base = "http://www-igpp.llnl.gov/leigh-cgi/g2d5.pl";
  17.  
  18.  
  19.  
  20.       public void init() {
  21.         int i;
  22.         int j;
  23.         Panel p = new Panel();
  24.         graph = new Graph2D();
  25.  
  26.         range = new Panel();
  27.         range.setLayout( new GridLayout(3,2,0,5) );
  28.  
  29.         range.add(new Label("Period"));
  30.         period = new TextField(8);
  31.         range.add(period);
  32.  
  33.         range.add(new Label("Amplitude"));
  34.         amplitude = new TextField(8);
  35.         range.add(amplitude);
  36.         range.add(new Button("Redraw"));
  37.         p.add(range);
  38.  
  39.  
  40.         setLayout( new BorderLayout() );
  41.         add("East", p);
  42.         add("Center", graph);
  43.  
  44.         graph.setFont(new Font("TimesRoman",Font.PLAIN,16));
  45.         
  46.  
  47.         try {
  48.         data1 = graph.loadFile(buildURL());
  49.         } catch (Exception e) {
  50.           System.out.println("Failed to load data file!");
  51.         }
  52.  
  53.         data1.linecolor = new Color(255,255,0);
  54.  
  55.         xaxis = graph.createAxis(Axis.BOTTOM);
  56.         xaxis.attachDataSet(data1);
  57.         xaxis.title = new String("Period");
  58.         xaxis.title_color = Color.magenta;
  59.  
  60.  
  61.         yaxis = graph.createAxis(Axis.LEFT);
  62.         yaxis.attachDataSet(data1);
  63.         yaxis.title = new String("Amplitude");
  64.         yaxis.title_offset = new Dimension(-10,-10);
  65.         yaxis.title_color = Color.magenta; 
  66.  
  67.       }
  68.  
  69.       public void paint(Graphics g) {
  70.            graph.paint(g);
  71.       }
  72.  
  73.  
  74.       public URL buildURL() {
  75.             URL url;
  76.  
  77.             StringBuffer s = new StringBuffer(base);
  78.             s.append("?amplitude=");
  79.             s.append(amplitude_new);
  80.             s.append("&period=");
  81.             s.append(period_new);
  82.  
  83.             try {
  84.                   url = new URL(s.toString());
  85.             } catch (Exception e) {
  86.                   url = null;
  87.             }
  88.  
  89.             return url;
  90.       }
  91.  
  92.       public boolean action(Event ev, Object arg) {
  93.            int i;
  94.  
  95.            if ("Redraw".equals(arg)) {
  96.  
  97.  
  98.  
  99.               if( amplitude.getText() == null &&
  100.                   period.getText() == null ) { return true; }
  101.  
  102.              if( amplitude.getText() != null ) {
  103.                   try {
  104.                      amplitude_new = Integer.parseInt(amplitude.getText());
  105.                   } catch(Exception e) { }
  106.               }
  107.              if( period.getText() != null ) {
  108.                   try {
  109.                      period_new = Integer.parseInt(period.getText());
  110.                   } catch(Exception e) { }
  111.               }
  112.  
  113.  
  114.               graph.deleteDataSet(data1);
  115.               data1 = graph.loadFile( buildURL() );
  116.               xaxis.attachDataSet(data1);
  117.               yaxis.attachDataSet(data1);
  118.               data1.linecolor = new Color(255,255,0);
  119.  
  120.  
  121.               graph.repaint();
  122.               return true;
  123.            } 
  124.  
  125.            return false;
  126.      }
  127.  
  128.  
  129.  
  130. }
  131.